package main

import "fmt"

//基础的参数结构体
type Option struct {
	Name    string
	Gender  string
	Age     int
	Company string
}

// 函数式选项定义
type OptionFunc func(*Option)

// 修改基础参数func
func ModifyName(name string) OptionFunc {
	return func(o *Option) {
		o.Name = name
	}
}
func ModifyGender(gender string) OptionFunc {
	return func(o *Option) {
		o.Gender = gender
	}
}
func ModifyAge(age int) OptionFunc {
	return func(o *Option) {
		o.Age = age
	}
}
func ModifyCompany(company string) OptionFunc {
	return func(o *Option) {
		o.Company = company
	}
}

// 默认参数初始化 必须是指针类型
var (
	defaultOption = &Option{
		Name:    "hexing",
		Gender:  "Man",
		Age:     26,
		Company: "Info",
	}
)

// 可变化的参数使用
func NewOption(opts ...OptionFunc) (opt *Option) {
	opt = defaultOption
	for _, o := range opts {
		o(opt)
	}
	return
}

// main test
func main() {
	x := NewOption(ModifyName("Greg"), ModifyCompany("IBM"))
	fmt.Println(x)

}
